package com.chinaoryx.xml;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.channels.FileChannel;
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class FileCopy {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FileCopy.class);
	
	
	public void copyFiles(String xmlFile, String dirName, String sqlBaseDir, String svnUrl) throws DocumentException, URISyntaxException, IOException{
		SAXReader reader = new SAXReader();
		
		Reader in = null;
		in = new InputStreamReader(new FileInputStream(xmlFile), "UTF-8");
        Document document = reader.read(in);
        
        List list = document.selectNodes( "//paths/path" );
        for (Object object : list) {
        	Element element = (Element) object;
        	String kind = element.attribute("kind").getText();
        	String item = element.attribute("item").getText();
        	if (!"file".equals(kind)) {
				continue;
			}
        	if ("deleted".equals(item)) {
				continue;
			}
        	
        	
        	String fileName = element.getText();
//        	URI uri = null;
//        	try {
//				uri = new URI(fileName);
//			} catch (Exception e) {
//				
//				System.out.println(e);
//				continue;
//			}
//			String path = null;
//			
//			path = uri.getPath();
////			uri.
//			
//			URLDecoder decoder = null;
			
			fileName = URLDecoder.decode(fileName, "UTF-8");
			
			//    remove the "/svn/havi/Design/DB"
			String path = fileName.substring(svnUrl.length());
			
			String bakFile = dirName + path;
			
			path = sqlBaseDir + path;
			File sqlFile = new File(path);
			
			
			FileChannel inChannel = null;
			FileChannel outChannel = null;
			inChannel = new FileInputStream(sqlFile).getChannel();
			
			int index = bakFile.lastIndexOf("/");
			String dirBakFileName = bakFile.substring(0, index);
			File dirBakFile = new File(dirBakFileName);
			dirBakFile.mkdirs();
			
			File outFile = new File(bakFile);
//			System.out.println(outFile);
			
			outFile.createNewFile();
//			outFile.mkdirs();
			
			outChannel = new FileOutputStream(outFile).getChannel();
			inChannel.transferTo(0, inChannel.size(), outChannel);
			
			logger.info("coped file:" + bakFile);
			
			
			
			
		}
        
	}
	
	
	public static void main(String[] args) throws DocumentException, URISyntaxException, IOException, ConfigurationException {
		
		Configuration config  = new PropertiesConfiguration("config.properties");
		
		String xmlFile = "filelist.xml";
		String dirName = config.getString("tmp.basedir");		
		String sqlBaseDir = config.getString("sql.basedir");
		String svnUrl = config.getString("svn.url");
		FileCopy obj = new FileCopy();
		obj.copyFiles(xmlFile, dirName, sqlBaseDir, svnUrl);
		
	}

}
